package hoan.nguyen.service.impl;

import hoan.nguyen.service.EmployeeService;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.Response;

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.stereotype.Service;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{

	@Override
	public String doGetRequest(String endPoint, String jsonData) {

		String jsonResponseData = "";
		
		WebClient client = WebClient.create(endPoint);
		try {
			Response r = client.accept("application/json").get();
			if (r.getStatus() == Response.Status.OK.getStatusCode()) {
				jsonResponseData = IOUtils.toString((InputStream) r.getEntity());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return jsonResponseData;
	}
	
	/**
	 * 
	 */
	public String doPostRequest(String serviceEndpoint, String jsonData) {
        
		List<Object> providers = new ArrayList<Object>();
		providers.add(new org.codehaus.jackson.jaxrs.JacksonJsonProvider());
		WebClient client = WebClient.create(serviceEndpoint, providers);

		try {
			Response r = client.accept("application/json").type("application/json").post(jsonData);
			if (r.getStatus() == Response.Status.OK.getStatusCode()) {
				return IOUtils.toString((InputStream) r.getEntity());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
    }

}
